package dao;

import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashSet;
import java.util.Set;

import models.Frequencia;


public class DAOFrequencia {

	public Set<Frequencia> select() throws Exception {
		Set<Frequencia> objj = new HashSet<Frequencia>();
		try {

			String query = "select idFrequencia, idUsuario, data from Frequencia";

			Statement statement = DAOConnection.getConnection().createStatement();

			ResultSet resultSet = statement.executeQuery(query);

			while(resultSet.next()) {
				Frequencia obj = new Frequencia();
				obj.idFrequencia = resultSet.getInt(1);
				obj.idUsuario = resultSet.getInt(2);
				obj.data = resultSet.getDate(3);
				objj.add(obj);
			}
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
		return objj; 
	}

	public Frequencia select(int idFrequencia) throws Exception {
		Frequencia obj = null;
		try	{

			String query = "select idFrequencia, idUsuario, data from Frequencia where id = ?";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(query);
			statement.setInt(1, idFrequencia);
			ResultSet resultSet = statement.executeQuery();

			while(resultSet.next()) {
				obj = new Frequencia();
				obj.idFrequencia = resultSet.getInt(1);
				obj.idUsuario = resultSet.getInt(2);
				obj.data = resultSet.getDate(3);
			}
		} catch(SQLException e) {
			e.printStackTrace();
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
		return obj; 
	}

	public void insert(Frequencia f) throws Exception {
		try {
			String insert = "insert into Frequencia(idUsuario, data) values(?, ?)";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(insert);

			statement.setInt(1, f.idUsuario);
			statement.setDate(2, (Date) f.data);

			statement.executeUpdate();
		} catch(SQLException e) {	
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}

	}

	public void update(Frequencia f) throws Exception {
		try {

			String update = "update Frequencia set idUsuario = ?, data = ? where idFrequencia = ?";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(update);

			statement.setInt(1, f.idUsuario);
			statement.setDate(2, (Date) f.data);
			statement.setInt(3, f.idFrequencia);


			statement.executeUpdate();
		} catch(SQLException e) {
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}

	}

	public void delete(Frequencia f) throws Exception {
		try {

			String delete = "delete from Frequencia where idFrequencia = ?";

			PreparedStatement statement = DAOConnection.getConnection().prepareStatement(delete);

			statement.setInt(1, f.idFrequencia);

			statement.executeUpdate();
		} catch(SQLException e) {
			throw new Exception();
		} finally {
			DAOConnection.closeConnection();
		}
	}

}
